package com.chouxiaozi.wechatpublic.handle;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weixin4j.Configuration;
import org.weixin4j.model.message.Articles;
import org.weixin4j.model.message.OutputMessage;
import org.weixin4j.model.message.event.SubscribeEventMessage;
import org.weixin4j.model.message.output.NewsOutputMessage;
import org.weixin4j.spi.DefaultEventMessageHandler;

import java.util.ArrayList;
import java.util.List;
/**
 * 事件处理器
 * 
 * @author jackly
 * 
 */
public class CustomEventMessageHandler extends DefaultEventMessageHandler {
	
	
	static Logger logger = LoggerFactory.getLogger(CustomEventMessageHandler.class);
	
	private static final String url = Configuration.getProperty("weixin4j.interface.url");

	
	public CustomEventMessageHandler() {
	}

	@Override
	public OutputMessage subscribe(SubscribeEventMessage msg) {

		Articles articles = new Articles();
		articles.setTitle("关注有礼");
		articles.setDescription("点击查看详情");

		articles.setPicUrl("http://www.baidu.com");

		articles.setUrl("http://www.baidu.com");


		logger.debug(JSON.toJSONString(articles));

		List<Articles> list = new ArrayList<Articles>();
		list.add(articles);

		// 实例化一个图文消息
		NewsOutputMessage newsOutputMsg = new NewsOutputMessage();
		newsOutputMsg.setArticles(list);
		return newsOutputMsg;
	}
	
	/**
	 * 获取场景ID
	 * @param eventKey
	 * @return
	 */
	private String getSceneId(String eventKey){
		
		String sceneId = "";
		if(StringUtils.isEmpty(eventKey)) {
			
			return sceneId;
		}
		if(eventKey.startsWith("qrscene_")) {
			sceneId = eventKey.split("_")[1];
		} else {
			sceneId = eventKey;
		}
		
		return sceneId;
	}
	
	
}
